package com.chuangjiangx.member.manager.client.web.score.controller;

import com.chuangjiangx.commons.response.CamelResponse;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.client.web.score.request.ClaimCodeValidationHistoryRequest;
import com.chuangjiangx.member.manager.client.web.score.request.ClaimCodeValidationRequest;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.score.ddd.application.MbrScoreGiftRecordApplication;
import com.chuangjiangx.member.score.ddd.application.command.ClaimCodeValidationCommand;
import com.chuangjiangx.member.score.ddd.application.result.ClaimCodeValidationResult;
import com.chuangjiangx.member.score.ddd.dal.condition.QueryClaimCodeValidationHistoryCondition;
import com.chuangjiangx.member.score.ddd.query.MbrScoreGiftRecordQuery;
import com.chuangjiangx.member.score.ddd.query.dto.ClaimCodeValidationHistory;
import com.chuangjiangx.member.score.ddd.query.dto.ClaimCodeValidationHistoryDetail;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cj-ch
 * @date 2018/7/4 下午6:27
 * 积分兑换记录
 */
@Api(value = "积分兑换记录" , tags = "积分")
@RestController
@RequestMapping(value = "/mbr/score-gift-record",produces = MediaType.APPLICATION_JSON_VALUE)
public class MbrScoreGiftRecordClientController {


    @Autowired
    private MbrScoreGiftRecordApplication mbrScoreGiftRecordApplication;

    @Autowired
    private MbrScoreGiftRecordQuery mbrScoreGiftRecordQuery;

    /**
     * 核销取货码
     * @param request
     * @return
     */
    @ApiOperation(value = "核销取货码")
    @RequestMapping(value = "/validation",method = RequestMethod.POST)
    @Login
    public CamelResponse<ClaimCodeValidationResult> validation(@RequestBody ClaimCodeValidationRequest request){
        ThreadLocalUser token = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);


        ClaimCodeValidationCommand command = new ClaimCodeValidationCommand();
        command.setMerchantId(token.getMerchantId());
        command.setMerchantUserId(token.getMerchantUserId());
        command.setClaimCode(request.getClaimCode());
        command.setTerminal(request.getTerminal());
        command.setStoreId(token.getStoreId());
        command.setStoreUserId(token.getStoreUserId());


        ClaimCodeValidationResult result = mbrScoreGiftRecordApplication.validation(command);
        return (CamelResponse<ClaimCodeValidationResult>)ResponseUtils.successCamel(result);
    }

    /**
     * 查询取货码核销记录
     * @param request
     * @return
     */
    @ApiOperation(value = "查询取货码核销记录")
    @RequestMapping(value = "/validation-history",method = RequestMethod.POST)
    @Login
    public CamelResponse<List<ClaimCodeValidationHistory>> validationHistory(@RequestBody ClaimCodeValidationHistoryRequest request){

        ThreadLocalUser token = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);

        QueryClaimCodeValidationHistoryCondition condition = new QueryClaimCodeValidationHistoryCondition();
        condition.setMerchantId(token.getMerchantId());
        condition.setClaimStatus(1);
        condition.setClaimDate(request.getDate());
        //会员三期新增字段
        condition.setCodeOrMobile(request.getCodeOrMobile());
        condition.setClaimStartDate(request.getClaimStartDate());
        condition.setClaimEndDate(request.getClaimEndDate());
        condition.setStoreId(token.getStoreId());
        condition.setStoreUserId(token.getStoreUserId());
        List<ClaimCodeValidationHistory> list = mbrScoreGiftRecordQuery.findClaimCodeValidationHistoryList(condition);
        return (CamelResponse<List<ClaimCodeValidationHistory>>)ResponseUtils.successCamel(list);
    }

    /**
     * 查询取货码核销详情
     */
    @RequestMapping(value = "/validation-detail/{id}",method = {RequestMethod.GET,RequestMethod.POST})
    @Login
    @ApiOperation(value = "查询取货码核销详情")
    public CamelResponse<ClaimCodeValidationHistoryDetail> validationDetail(@ApiParam(value = "取货码") @PathVariable Long id){
        ClaimCodeValidationHistoryDetail detail = mbrScoreGiftRecordQuery.findClaimCodeValidationHistoryDetail(id);
        return (CamelResponse<ClaimCodeValidationHistoryDetail>)ResponseUtils.successCamel(detail);
    }
}
